/**
 * 
 */

var SEARCH_TYPE_CUSTOMER 		= 0;
var SEARCH_TYPE_MANAGER			= 1;
var SEARCH_TYPE_OPERATOR		= 2;
var SEARCH_TYPE_REPAIR_VENDOR	= 3;

var BIZ_TYPE_CUSTOMER		= "001";
var BIZ_TYPE_SUPPLIER		= "002";
var BIZ_TYPE_REPAIR_VENDOR	= "003";



function search(searchType, upCustId, useYn, custType, word) {
	
	var bizType = null;
	if ( searchType == SEARCH_TYPE_CUSTOMER ) {
		bizType = BIZ_TYPE_CUSTOMER;
	}
	else if ( searchType == SEARCH_TYPE_REPAIR_VENDOR ) {
		bizType = BIZ_TYPE_REPAIR_VENDOR;
	}
	else {
		bizType = BIZ_TYPE_SUPPLIER;
	}
	
	$.ajax({
		url: contextPath + "/customer/list",
		data: {bizType:bizType, upCustId:upCustId, useYn:useYn, custType:custType, word:word},
		type: 'GET',
		dataType: 'json'
	}).done(function(data) {
		$('#customer_table').bootstrapTable('load', data);
	})
};

function searchCustomer() {
	var useYn = $("#use_yn").val();
	if ( useYn.length == 0 ) {
		useYn = 'Y';
	}
	
	var custType = $("#customer_type").val();
	var word = $("#search_word").val();
	
	search(SEARCH_TYPE_CUSTOMER, null, useYn, custType, word);
}

function searchManager() {
	var useYn = $("#use_yn").val();
	if ( useYn.length == 0 ) {
		useYn = 'Y';
	}
	
	var custId = $("#cust_id").val();
	var custType = $("#customer_type").val();
	var word = $("#search_word").val();
	
	search(SEARCH_TYPE_MANAGER, custId, useYn, custType, word);
}

function searchOperator() {
	var useYn = $("#use_yn").val();
	if ( useYn.length == 0 ) {
		useYn = 'Y';
	}
	
	var custId = $("#cust_id").val();
	var custType = $("#customer_type").val();
	var word = $("#search_word").val();
	
	search(SEARCH_TYPE_OPERATOR, custId, useYn, custType, word);
}

function searchRepairVendor() {
	var useYn = $("#use_yn").val();
	if ( useYn.length == 0 ) {
		useYn = 'Y';
	}
	
	var custId = $("#cust_id").val();
	var custType = $("#customer_type").val();
	var word = $("#search_word").val();
	
	search(SEARCH_TYPE_REPAIR_VENDOR, custId, useYn, custType, word);
}

function loadBizType() {
	$.ajax({
		url: contextPath + '/cm_common/list',
		data: {companyCode:'1000', majorCode:'TMO12'},
		type: 'GET',
		dataType: 'json'
	}).done(function(data) {
		if ( data != null && data.length > 0 ) {
			$("#biz_type").append($('<option>', { value: '', text: '' }));
			
			$.each(data, function(i, o) {
				$("#biz_type").append($('<option>', { value: o['minorCode'], text: o['minorName'] }));
			});
		}
	});
}

function loadUseYn() {
	$.ajax({
		url: contextPath + '/cm_common/list',
		data: {companyCode:'1000', majorCode:'TMO11'},
		type: 'GET',
		dataType: 'json'
	}).done(function(data) {
		if ( data != null && data.length > 0 ) {
			$("#use_yn").append($('<option>', { value: '', text: '' }));
			
			$.each(data, function(i, o) {
				$("#use_yn").append($('<option>', { value: o['minorCode'], text: o['minorName'] }));
			});
		}
	});
}

function loadCustType() {
	$.ajax({
		url: contextPath + '/cm_common/list',
		data: {companyCode:'1000', majorCode:'TMO10'},
		type: 'GET',
		dataType: 'json'
	}).done(function(data) {
		if ( data != null && data.length > 0 ) {
			$("#customer_type").append($('<option>', { value: '', text: '' }));
			
			$.each(data, function(i, o) {
				$("#customer_type").append($('<option>', { value: o['minorCode'], text: o['minorName'] }));
			});
		}
	});
}

function initCustomerPopupUI(searchType, title) {
	var html = [];
	html.push('<div class="modal-header">');
	html.push('	<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>');
	html.push('	<h4 class="modal-title" id="my_modal_label">' + title + '</h4>');
	html.push('</div>');
	html.push('<div class="modal-body">');
	html.push(' <div class="form-inline">');
	html.push(' 	<div class="row">');

	if ( searchType == SEARCH_TYPE_CUSTOMER || searchType == SEARCH_TYPE_REPAIR_VENDOR ) {
		html.push('			<div class="col-sm-6">');
		html.push('				<div class="form-group form-group-sm">');
		html.push('					<label class="control-label" for="biz_type" style="width:100px">사업자 구분</label>');
		html.push('					<select class="form-control" id="biz_type" style="width:200px" disabled>');

		if ( searchType == SEARCH_TYPE_CUSTOMER ) {
			html.push('						<option value="001">고객</option>');
		}
		if ( searchType == SEARCH_TYPE_REPAIR_VENDOR ) {
			html.push('						<option value="003">수리처</option>');
		}
		
		html.push('					</select>');
		html.push('				</div>');
		html.push('			</div>');
		html.push('			<div class="col-sm-6">');
		html.push('				<div class="form-group form-group-sm">');
		html.push('					<label class="control-label" for="use_yn" style="width:100px">사용여부</label>');
		html.push('					<select class="form-control" id="use_yn" style="width:200px">');
		html.push('					</select>');
		html.push('				</div>');
		html.push('			</div>');
	}
	else {
		html.push('			<div class="col-sm-6">');
		html.push('				<div class="form-group form-group-sm">');
		html.push('					<label class="control-label" for="cust_id" style="width:100px">고객</label>');
		html.push('					<input type="text" class="form-control" id="cust_id" style="width:100px" readonly></input>');
		html.push('					<input type="text" class="form-control" id="cust_name" style="width:100px" readonly></input>');
		html.push('				</div>');
		html.push('			</div>');
		html.push('			<div class="col-sm-6">');
		html.push('				<div class="form-group form-group-sm">');
		html.push('					<label class="control-label" for="use_yn" style="width:100px">사용여부</label>');
		html.push('					<select class="form-control" id="use_yn" style="width:200px">');
		html.push('					</select>');
		html.push('				</div>');
		html.push('			</div>');
	}
	
	html.push('		</div>');
	html.push(' 	<p>');
	html.push(' 	<div class="row">');
	html.push('			<div class="col-sm-6">');
	html.push('				<div class="form-group form-group-sm">');
	html.push('					<label class="control-label" for="customer_type" style="width:100px">고객유형분류</label>');
	html.push('					<select class="form-control" id="customer_type" style="width:200px">');
	html.push('					</select>');
	html.push('				</div>');
	html.push('			</div>');
	html.push('			<div class="col-sm-6">');
	html.push('				<div class="form-group form-group-sm">');
	html.push('					<label class="control-label" for="search_word" style="width:100px">조회</label>');
	html.push('					<input type="text" class="form-control" id="search_word" style="width:200px"></input>');
	
	if ( searchType == SEARCH_TYPE_CUSTOMER ) {
		html.push('					<input type="button" class="btn btn-primary btn-sm" id="search_btn" value="검색" style="width:60px" onclick="searchCustomer()"></input>');
	}
	else if ( searchType == SEARCH_TYPE_REPAIR_VENDOR ) {
		html.push('					<input type="button" class="btn btn-primary btn-sm" id="search_btn" value="검색" style="width:60px" onclick="searchRepairVendor()"></input>');
	}
	else if ( searchType == SEARCH_TYPE_MANAGER ) {
		html.push('					<input type="button" class="btn btn-primary btn-sm" id="search_btn" value="검색" style="width:60px" onclick="searchManager()"></input>');
	}
	else if ( searchType == SEARCH_TYPE_OPERATOR ) {
		html.push('					<input type="button" class="btn btn-primary btn-sm" id="search_btn" value="검색" style="width:60px" onclick="searchOperator()"></input>');
	}
	
	html.push('				</div>');
	html.push('			</div>');
	html.push('		</div>');
	html.push('	</div>');
	html.push('	<hr style="width:100%; height:1px; background-color:lightgray;"/>');
	html.push('	<table id="customer_table" data-click-to-select="true" data-height="300" data-pagination="true" data-search="false">');
	html.push('		<thead>');
	html.push('			<tr>');
	html.push('				<th data-radio="true"></th>');
	html.push('				<th data-field="custName">고객명</th>');
	html.push('				<th data-field="custAccNo">고객코드</th>');
	html.push('				<th data-field="ceoName">대표자명</th>');
	html.push('				<th data-field="bizTypeName">사업자 구분</th>');
	html.push('				<th data-field="custTypeName">고객유형분류</th>');
	html.push('				<th data-field="useYnName">사용여부</th>');
	html.push('				<th data-field="repairCustCode">수리처코드</th>');
	html.push('			</tr>');
	html.push('		</thead>');
	html.push('		<tbody>');
	html.push('		</tbody>');
	html.push('	</table>');
	html.push('</div>');
	html.push('<div class="modal-footer">');
	html.push('<button type="button" class="btn btn-primary" id="confirm">확인</button>');
	html.push('<button type="button" class="btn btn-default" data-dismiss="modal">취소</button>');
	html.push('</div>');
	
	$('div.modal-content').html(html.join(''));
	
	// bootstrapTable 적용
	$('#customer_table').bootstrapTable();
	
	$('#customer_table').dblclick(function(event) {
		var row = $(event.target).closest('tr');
		
		if ( searchType == SEARCH_TYPE_CUSTOMER ) {
			var selectedName = row.find('td:nth(1)').text().trim();
			var selectedCode = row.find('td:nth(2)').text().trim();
			
			$('#customer_code').val(selectedCode);
			$('#customer_name').val(selectedName);
		}
		else if ( searchType == SEARCH_TYPE_MANAGER ) {
			var selectedName = row.find('td:nth(1)').text().trim();
			var selectedCode = row.find('td:nth(2)').text().trim();
			
			$('#manager_code').val(selectedCode);
			$('#manager_name').val(selectedName);
		}
		else if ( searchType == SEARCH_TYPE_OPERATOR ) {
			var selectedName = row.find('td:nth(1)').text().trim();
			var selectedCode = row.find('td:nth(2)').text().trim();
			
			$('#operator_code').val(selectedCode);
			$('#operator_name').val(selectedName);
		}
		else if ( searchType == SEARCH_TYPE_REPAIR_VENDOR ) {
			var selectedName = row.find('td:nth(1)').text().trim();
			var selectedCode = row.find('td:nth(7)').text().trim();
			
			$('#repair_vendor_code').val(selectedCode);
			$('#repair_vendor_name').val(selectedName);
		}

		$('#modal_div').modal("hide");
	});

	
	$('#confirm').click(function() {
		var selectedCustomerCode = "";
		var selectedCustomerName = "";

		var trList = $('#customer_table tr');

		trList.each(function(i, e) {
			var checkFlag = $(e).find('input:radio').prop('checked');
			
			if ( checkFlag == true ) {
				if ( searchType == SEARCH_TYPE_CUSTOMER ) {
					selectedName = $(e).children("td:nth(1)").text().trim();
					selectedCode = $(e).children("td:nth(2)").text().trim();
					
					$('#customer_code').val(selectedCode);
					$('#customer_name').val(selectedName);
				}
				else if ( searchType == SEARCH_TYPE_MANAGER ) {
					selectedName = $(e).children("td:nth(1)").text().trim();
					selectedCode = $(e).children("td:nth(2)").text().trim();
					
					$('#manager_code').val(selectedCode);
					$('#manager_name').val(selectedName);
				}
				else if ( searchType == SEARCH_TYPE_OPERATOR ) {
					selectedName = $(e).children("td:nth(1)").text().trim();
					selectedCode = $(e).children("td:nth(2)").text().trim();
					
					$('#operator_code').val(selectedCode);
					$('#operator_name').val(selectedName);
				}
				else if ( searchType == SEARCH_TYPE_REPAIR_VENDOR ) {
					selectedName = $(e).children("td:nth(1)").text().trim();
					selectedCode = $(e).children("td:nth(7)").text().trim();
					
					$('#repair_vendor_code').val(selectedCode);
					$('#repair_vendor_name').val(selectedName);
				}
				
			}
		});
		
		$('#modal_div').modal("hide");
	});
	
	$('#modal_div .modal-dialog').addClass("modal-lg");
	
	$('#modal_div').modal("show");	
}

$(function() {
	
	$("#customer_btn").click(function() {
		initCustomerPopupUI(SEARCH_TYPE_CUSTOMER, "고객 조회");

		//loadBizType();
		loadCustType();
		loadUseYn();
	});
	
	$("#manager_btn").click(function() {
		initCustomerPopupUI(SEARCH_TYPE_MANAGER, "관리국 조회");

		var custId = $("#customer_code").val();
		var custName = $("#customer_name").val();
		
		$("#cust_id").val(custId);
		$("#cust_name").val(custName);
		
		//loadBizType();
		loadCustType();
		loadUseYn();
	});
	
	$("#operator_btn").click(function() {
		initCustomerPopupUI(SEARCH_TYPE_OPERATOR, "운용국 조회");

		var custId = $("#customer_code").val();
		var custName = $("#customer_name").val();
		
		$("#cust_id").val(custId);
		$("#cust_name").val(custName);
		
		//loadBizType();
		loadCustType();
		loadUseYn();
	});
	
	$("#repair_vendor_btn").click(function() {
		initCustomerPopupUI(SEARCH_TYPE_REPAIR_VENDOR, "수리처 조회");

		//loadBizType();
		loadCustType();
		loadUseYn();
	});
})